package com.socialsoftware.model;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

@Entity(tableName = "contacts")
public class Contact implements Parcelable {
    @PrimaryKey
    @NonNull
    private String id;
    private String name;
    private String avatar;
    private String pinyin; // For sorting and indexing
    private String firstLetter; // First letter for sidebar indexing
    private boolean isStarred; // For starred contacts (like WeChat's top contacts)
    private String remark; // Custom remark name
    private String region; // Region/location information
    
    // Special types
    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_HEADER = 1; // For section headers
    public static final int TYPE_SPECIAL = 2; // For special entries like "New Friends" or "Groups"
    
    private int type = TYPE_NORMAL;
    private String specialIcon; // Icon for special entries
    
    public Contact() {
        // Required empty constructor for Room
    }
    
    @Ignore
    public Contact(String id, String name, String avatar) {
        this.id = id;
        this.name = name;
        this.avatar = avatar;
        this.firstLetter = getFirstLetter(name);
    }
    
    @Ignore
    public Contact(int type, String name, String specialIcon) {
        this.type = type;
        this.name = name;
        this.specialIcon = specialIcon;
        this.id = "special_" + name; // Generate unique ID for special contacts
    }
    
    protected Contact(Parcel in) {
        id = in.readString();
        name = in.readString();
        avatar = in.readString();
        pinyin = in.readString();
        firstLetter = in.readString();
        isStarred = in.readByte() != 0;
        remark = in.readString();
        region = in.readString();
        type = in.readInt();
        specialIcon = in.readString();
    }
    
    private String getFirstLetter(String name) {
        if (name == null || name.isEmpty()) {
            return "#";
        }
        // Simple implementation - in real app, would use proper pinyin conversion
        char first = name.charAt(0);
        if (Character.isLetter(first)) {
            return String.valueOf(Character.toUpperCase(first));
        }
        return "#";
    }
    
    // Getters and setters
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
        this.firstLetter = getFirstLetter(name);
    }
    
    public String getAvatar() {
        return avatar;
    }
    
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
    
    public String getPinyin() {
        return pinyin;
    }
    
    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }
    
    public String getFirstLetter() {
        return firstLetter;
    }
    
    public void setFirstLetter(String firstLetter) {
        this.firstLetter = firstLetter;
    }
    
    public boolean isStarred() {
        return isStarred;
    }
    
    public void setStarred(boolean starred) {
        isStarred = starred;
    }
    
    public String getRemark() {
        return remark;
    }
    
    public void setRemark(String remark) {
        this.remark = remark;
    }
    
    public String getRegion() {
        return region;
    }
    
    public void setRegion(String region) {
        this.region = region;
    }
    
    public int getType() {
        return type;
    }
    
    public void setType(int type) {
        this.type = type;
    }
    
    public String getSpecialIcon() {
        return specialIcon;
    }
    
    public void setSpecialIcon(String specialIcon) {
        this.specialIcon = specialIcon;
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(name);
        dest.writeString(avatar);
        dest.writeString(pinyin);
        dest.writeString(firstLetter);
        dest.writeByte((byte) (isStarred ? 1 : 0));
        dest.writeString(remark);
        dest.writeString(region);
        dest.writeInt(type);
        dest.writeString(specialIcon);
    }
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    public static final Creator<Contact> CREATOR = new Creator<Contact>() {
        @Override
        public Contact createFromParcel(Parcel in) {
            return new Contact(in);
        }
        
        @Override
        public Contact[] newArray(int size) {
            return new Contact[size];
        }
    };

    @Override
    public String toString() {
        return "Contact{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", avatar='" + avatar + '\'' +
                ", pinyin='" + pinyin + '\'' +
                ", firstLetter='" + firstLetter + '\'' +
                ", isStarred=" + isStarred +
                ", remark='" + remark + '\'' +
                ", region='" + region + '\'' +
                ", type=" + type +
                ", specialIcon='" + specialIcon + '\'' +
                '}';
    }
}